<?php

namespace App\Http\Controllers\Frontend\V10\UnionLogin;

use App\Library\String\UnionLoginTools;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Constant\SmsTemplate;
use App\Library\String\Strings;
class WycardController extends Controller
{

//    const LOAD_URLS = "https://manage.longloan.cn/#/fish";
    const LOAD_URLS = "https://manage.longloan.cn/#/download";
    
    /**
     * 撞库返回code
     */

    const HIT_CODE_SUCCESS_NEW = 200; //新用户
    const HIT_CODE_FAILURE_OLD = 1000;//老用户
    const HIT_CODE_FAILURE_SIGN = 1003;//签名错误
    const HIT_CODE_FAILURE_FROM = 1004;//其他渠道



    /**
     * 连登返回code
     */
    const UINON_CODE_SUCCESS = 200; //联合登录成功（返回url）
    const UINON_CODE_ERROR = 1001; //联合登录失败
    const UINON_CODE_ERROR_SER = 1002; //服务器内部错误
    const UINON_CODE_ERROR_SIGN = 1003; //签名错误
    const UINON_CODE_ERROR_OLD = 1000; //老用户


    /**
     * 连登返回其他错误
     */
    const GATHER_CODE_FAILURE_THINGS = 1002;//其他错误

    const GATHER_CODE_FAILURE_MESS = 1005;//参数错误



    public function index(Request $request){
        $userModel = new User();
        $userModel->setConnection('backend');
        $has_register = $userModel->where(['phone_num_md5'=>'6c708dd6d7f2efb3d7354f6630b07702'])->first();
        if (!empty($has_register)){
            if($has_register->source == 41002) {
                $code = self::HIT_CODE_FAILURE_OLD;
                $message = '本渠道老用户';
                return json_encode(['code'=>$code,'massage'=>$message],JSON_UNESCAPED_UNICODE);
            }else{
                $code = self::HIT_CODE_FAILURE_FROM;
                $message = '其他渠道老用户';
                return json_encode(['code'=>$code,'massage'=>$message],JSON_UNESCAPED_UNICODE);
            }
        }

        //dd($has_register);
        //$sign1 = $request->input('sign');
        //var_dump($sign1);
        //$str = 'lKqnFa6zuyXkv6p7GLPrb1DzVb9ftc4rz4ypFD1NqlfvyxaaIC3rCEFk8JF0TW6EojQc4el45CtB\nbuiyfmxNd7/N1bnQiZ03LcY25A1dkWwXQaDw1yuyYdQoa+pJqp0RGzeIdz0hXYwrDQJ6Fxet20Ty\nMT2C4gFCe74CAHzDPp0=\n';
//        $data = $request->data;
        //var_dump($str);
        //$data = [
        //    "mid"=>"juxun",
        //    "phone"=>"15898569663",
        //    'ip' => '192.168.1.1'
        //];
        //加密
        //$sign = UnionLoginTools::encryptGatherBank($data);

        //var_dump($sign);

    }
    /**
     * Description: 接收51信用卡用户验证
     * Author: ShengZengGuang
     * DateTime: 2019-03-15 11:43
     * @param Request $request
     * @return mixed
     */
    public function check(Request $request)
    {
        try{
            $data_save['timestamp'] = $request->input('timestamp','');
            $data_save['enmobile'] = $request->input('mobile','');
            $data_save['mobileMd5'] = $request->input('mobileMd5','');
            $data_save['appId'] = $request->input('appId','');
            $data_save['channelId'] = $request->input('channelId','');
            $data_save_sign = $request->input('sign', '');
            //$data_save_sign = rawurlencode(rawurlencode($data_save_sign));
            //$data_save_sign = rawurldecode($data_save_sign);

            if (empty($data_save['timestamp']) || empty($data_save['enmobile']) || empty($data_save['mobileMd5']) || empty($data_save_sign)){
                $code = self::GATHER_CODE_FAILURE_MESS;
                $message = '请求参数为空';
                return json_encode(['code'=>$code,'massage'=>$message],JSON_UNESCAPED_UNICODE);
            }


            //dd($data_save);
            //验签
            $has_pass = UnionLoginTools::WycardVerifyBank($data_save,$data_save_sign);
            if(!$has_pass){
                $code = self::HIT_CODE_FAILURE_SIGN;
                $message = '数据签名错误';
                return json_encode(['code'=>$code,'massage'=>$message],JSON_UNESCAPED_UNICODE);
            }
            $raw_mobile = $data_save['mobileMd5'];
            $userModel = new User();
            $userModel->setConnection('backend');
            //$has_register = $userModel->where(['phone_num_md5'=>$raw_mobile])->value('id');
            $has_register = $userModel->where(['phone_num_md5'=>$raw_mobile])->first();
            if (!empty($has_register)){
                if($has_register->source == 41002) {
                    $code = self::HIT_CODE_FAILURE_OLD;
                    $message = '本渠道老用户';
                    return json_encode(['code'=>$code,'massage'=>$message],JSON_UNESCAPED_UNICODE);
                }else{
                    $code = self::HIT_CODE_FAILURE_FROM;
                    $message = '其他渠道老用户';
                    return json_encode(['code'=>$code,'massage'=>$message],JSON_UNESCAPED_UNICODE);
                }
            }


            $code = self::HIT_CODE_SUCCESS_NEW;
            $message = '新用户';
            return json_encode(['code'=>$code,'massage'=>$message],JSON_UNESCAPED_UNICODE);
        }catch (\Exception $e){
            $code = self::GATHER_CODE_FAILURE_THINGS;
            return json_encode(['code'=>$code,'massage'=>$e->getMessage()],JSON_UNESCAPED_UNICODE);
        }
    }
    /**
     * Description: 接收51信用卡用户注册
     * Author: ShengZengGuang
     * DateTime: 2019-03-15 11:43
     * @param Request $request
     * @return mixed
     */
    public function user_register(Request $request)
    {
        $url = self::LOAD_URLS.'?channel='.User::SOURCE_51_CREDIT_CARD;
        try{

            $rec_timestamp = $data_save['timestamp'] = $request->input('timestamp','');
            $rec_mobile  = $data_save['mobile'] = $request->input('mobile','');
            $data_save_sign = $request->input('sign', '');
            if (empty($rec_timestamp) || empty($rec_mobile) || empty($data_save_sign)){
                $code = self::GATHER_CODE_FAILURE_MESS;
                $message = '请求参数为空';
                return json_encode(['code'=>$code,'massage'=>$message],JSON_UNESCAPED_UNICODE);
            }


            $sign_data = [
                'appId'=>$request->input('appId',''),
                'channelId'=>config('union_login.channelid'),
                'mobile'=>$rec_mobile,
                'timestamp'=>$rec_timestamp
            ];


            //验签
            $has_pass = UnionLoginTools::WycardVerifyBankregister($sign_data,$data_save_sign);
            if(!$has_pass){
                $code = self::UINON_CODE_ERROR_SIGN;
                $message = '数据签名错误';
                return json_encode(['code'=>$code,'massage'=>$message],JSON_UNESCAPED_UNICODE);
            }
            $mobile = $rec_mobile;
//            $username = $json_data['username'];
            if(User::where('phone_num',$mobile)->value('id')){
                $code = self::UINON_CODE_ERROR_OLD;
                $message = '已注册用户重复注册';
                return json_encode(['code'=>$code,'massage'=>$message,'url'=>$url],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
            }
            $rand_code =Strings::randomCode(6);
            User::createUser([
                'phone_num' => $mobile,
                'password' => $rand_code,
                'source' => User::SOURCE_51_CREDIT_CARD,
            ]);
           // $mobile = '15737893304';
            //推送51信用卡用户，快闪卡贷自动注册成功
            SmsTemplate::send(SmsTemplate::TEMPLATE_RZG_REGISTER,$mobile,['name'=>$mobile,'password'=>$rand_code]);
            $code = self::UINON_CODE_SUCCESS;
            $message = '成功';
            return json_encode(['code'=>$code,'massage'=>$message,'url'=>$url],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
        }catch (\Exception $e){
            $code = self::GATHER_CODE_FAILURE_THINGS;
            return json_encode(['code'=>$code,'massage'=>$e->getMessage()],JSON_UNESCAPED_UNICODE);
        }
    }
}
